package com.meteor.downloader.videodownloader.utils

import android.content.Context
import androidx.core.content.edit
import com.meteor.downloader.videodownloader.MdmDealApplication

object DataManagerMdmUtils {
    private const val SHARED_PREFERENCES_NAME = "MeteorDownloader"

    fun setDataKeyMdm(saveKey: String, saveData: Any) {
        try {
            val sp = MdmDealApplication.mMdmContext.getSharedPreferences(
                SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE
            )
            sp.edit {
                when (saveData) {
                    is String -> putString(saveKey, saveData)
                    is Float -> putFloat(saveKey, saveData)
                    is Long -> putLong(saveKey, saveData)
                    is Int -> putInt(saveKey, saveData)
                    is Boolean -> putBoolean(saveKey, saveData)
                }
            }
        } catch (_: Exception) {
        }
    }

    fun <T> getDataKeyMdm(getKey: String, defaultData: T): T {
        return try {
            val sp = MdmDealApplication.mMdmContext.getSharedPreferences(
                SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE
            )
            when (defaultData) {
                is String -> sp.getString(getKey, defaultData) as T
                is Float -> sp.getFloat(getKey, defaultData) as T
                is Long -> sp.getLong(getKey, defaultData) as T
                is Int -> sp.getInt(getKey, defaultData) as T
                is Boolean -> sp.getBoolean(getKey, defaultData) as T
                else -> defaultData
            }
        } catch (_: Exception) {
            defaultData
        }
    }
}